package com.jfinal.kit;


import java.util.Map;

/**
 * 为了兼容主流的json接口规范，主要定义的key为
 * status 状态码  200 为成功
 * code   为 0 的
 * message 提示的消息
 * data 存储的数据
 */
public class Result extends Ret {

    private static final String MESSAGE = "message";

    private static final String CODE = "code";

    private static final String DATA = "data";


    public Result set(Object key, Object value) {
        super.set(key, value);
        return this;
    }

    public static Result _new() {
        return new Result();
    }

    public static Result _new(Object key, Object value) {
        return _new().set(key, value);
    }

    public String getMessage(){
        return getStr(MESSAGE);
    }

    public void setMessage(String message){
        this.put(MESSAGE,message);
    }

    public String getMessage(String defValue){
        return getMessage();
    }
    /**
     * 获得数据
     *
     * @param <T>
     * @return
     */
    public <T> T getData() {
        try {
            return (T) get(DATA);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;
    }

    public <T> T getCode() {
        return getValue(CODE);
    }


    public <T> T getValue(Object key) {
        try {
            return (T) get(key);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;
    }

    public <T> T getOrDef(Object key, T defValue) {
        if (get(key) == null) {
            return defValue;
        }
        return getValue(key);
    }

    public <T> T getOrDef(T defValue, Object... key) {
        T t = getValue(key);
        if (t == null) {
            return defValue;
        }
        return t;
    }

    public <T> T get(Object... key) {
        if (key != null) {
            if (key.length > 1) {
                try {
                    Map<Object, Object> temp = this;
                    for (int i = 0; i < key.length - 1; i++) {
                        if (temp.get(key[i]) != null && temp.get(key[i]) instanceof Map) {
                            temp = ((Map<Object, Object>) temp.get(key[i]));
                        } else {
                            return null;
                        }
                    }
                    return (T) temp.get(key[key.length - 1]);
                } catch (Exception e) {
                    //e.printStackTrace();
                }
                return null;
            } else {
                return this.getValue(key[0]);
            }
        }
        return null;
    }


    public static void main(String[] args) {
        /*Result r = new Result();
        r.setData(new HMap().set("a", "a").set("b", new HMap().set("c", "c")));

        System.out.println(r);

        Map m = (Map) r.getData();

        System.out.println(m);
        System.out.println(r.get("data", "b", "c"));*/
    }

    public Result setOk() {
        return setOk(null);
    }

    public Result setOk(Object message) {
        this.set(CODE, 0);
        if (message != null) {
            this.set(MESSAGE, message);
        }
        return this;
    }

    public Result setSuccess() {
        return setOk();
    }

    public Result setSuccess(Object message) {
        return setOk(message);
    }

    public Result setFail(int code, Object message) {
        this.set(CODE, code);
        if (message != null) {
            this.set(MESSAGE, message);
        }
        return this;
    }

    public Result setFail(String code, Object message) {
        this.set(CODE, code);
        if (message != null) {
            this.set(MESSAGE, message);
        }
        return this;
    }

    /**
     * 默认成功的标识是  code == 0
     *
     * @return
     */
    public boolean isOk() {
        try {
            if (this.getInt(CODE) != null) {
                if (this.getInt(CODE) == 0) {
                    return true;
                }
            }
        } catch (Exception e) {

        }
        return false;
    }

    public boolean isFail() {
        return !isOk();
    }


    public Result setData(Object data) {
        return set("data", data);
    }
}
